package com.sky.hystrix.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sky.hystrix.service.IHystrixService;
import org.springframework.stereotype.Service;

@Service
public class HystrixServiceImpl implements IHystrixService {

    @HystrixCommand(fallbackMethod = "getOrderPageListFallback",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "2000"), //并发执行的最大线程数，默认10
                    //BlockingQueue(阻塞队列)的最大队列数，当设为－1，
                    // 会使用SynchronousQueue(同步队列)，值为正时使用LinkedBlockingQueue(链接的阻止队列)。
                    // 该设置只会在初始化时有效，之后不能修改threadpool的queue size，除非reinitialising thread executor(重新初始化线程执行器)。默认－1。
                    @HystrixProperty(name = "maxQueueSize", value = "4000"),
                    //即使maxQueueSize没有达到，达到queueSizeRejectionThreshold该值后，
                    // 请求也会被拒绝。因为maxQueueSize不能被动态修改，这个参数将允许我们动态设置该值。
                    // 如果maxQueueSize设置为-1，该字段将不起作用
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value ="100")},
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000"), //命令执行超时时间
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"), //若干10s一个窗口内失败三次, 则达到触发熔断的最少请求量
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000"), //断路30s后尝试执行, 默认为5s
                    @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "100"), // 回退方法的最大并发数
            })
    @Override
    public void testHystrix(){
        /*int num = 1;
        if(num == 1) {
            num = 0;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            num = 1;
        }*/
        System.out.println("正常访问，业务处理");
    }

    public void getOrderPageListFallback() {
        System.out.println("限流开始降级");
    }
}
